﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace Formsy.Common
{
    public static class Extensions
    {

        #region XElement Extensions
        public static string GetFieldType(this XElement field)
        {
            return field.Attribute("Type").Value;
        }


        public static string GetAttributeOrDefault(this XElement field, string name)
        {
            if (field.HasAttribute(name))
                return field.Attribute(name).Value;
            return null;
        }




        public static string GetName(this XElement field)
        {
            return field.GetAttributeOrDefault("Name");
        }


        public static string GetValue(this XElement field)
        {
            return field.GetAttributeOrDefault("Value");

        }


        public static string GetText(this XElement field)
        {
            return field.Attribute("Text").Value;
        }

        public static string GetLabel(this XElement field)
        {
            return field.Attribute("Label").Value;
        }


        public static bool HasAttribute(this XElement x, string name)
        {
            return x.Attribute(name) == null ? false : true;
        }


        #endregion

        /// <summary>
        /// Converts a string to lowercase and removes spaces.
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string Flatten(this string s)
        {
            return s.ToLower().Replace(" ", string.Empty);
        }


        public static string ToCommaDelimited(this string[] array)
        {
            string delimted = string.Join(", ", array);
            return delimted;
        }

    }
}
